package com.dongjiantong.app.controller.biz;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.biz.enums.DrugStoreType;
import com.dongjiantong.biz.model.DrugStore;
import com.dongjiantong.biz.service.DrugStoreService;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;


/**
 * Created by ThinkPad on 2017/12/11.
 */
@Slf4j
@OpenBean
public class DrugStoreController {
    @Reference(version = "1.0.0")
    private DrugStoreService drugStoreService;

    /**
     * @param type  1 药品,2疫苗
     * @param group 1猪2牛3羊4家禽
     * @return
     */
    @OpenMethod(
            key = "app.DrugStore.selectByType",
            paramNames = {"type", "group"},
            httpMethods = RequestMethod.POST)
    public Response<List<DrugStore>> selectByType(Integer type, Integer group) {

        DrugStore drugStore = new DrugStore();
        if (type != null) {
            drugStore.setType(type);

        }
        if (group != null) {
            drugStore.setGroup_type(group);
        }
        List<DrugStore> drugStoreList = drugStoreService.select(drugStore);

        return Response.ok(drugStoreList);

    }

    /**
     * 疫苗的列表 (包含code和name)
     * @return List<Map<String,Object>>
     */
    @OpenMethod(key = "app.drugStore.selectVaccineList",
                            paramNames = {"group"},
                            httpMethods = RequestMethod.GET)
    public Response<List<Map<String,Object>>> selectVaccineList(Integer group) {
        List<Map<String,Object>> result = Lists.newArrayList();
        Map<String,Object> map = Maps.newHashMap();

        map.put("type",DrugStoreType.DRUG_STORE_vaccine.getCode());
        if (group != null) {
            map.put("group_type",group);
        }

        result = drugStoreService.selectVaccineList(map);

        return Response.ok(result);
    }

    /**
     * 养殖档案获取药物种类,养殖档案获取疫苗种类
     * @return
     */
    @OpenMethod(key = "app.biz.obtainVaccineOrDrug",
            paramNames = {"name","pageNum","pageSize","type"},
            httpMethods = RequestMethod.GET)
    public Response<PageInfo<Map<String,Object>>> obtainVaccine(
                                                            @RequestParam(required = false) String name,
                                                            @RequestParam(required = false) Integer pageNum,
                                                            @RequestParam(required = false) Integer pageSize,
                                                            @RequestParam(required = false) Integer type){
        PageInfo<Map<String,Object>> pageInfo = drugStoreService.getDrugStore(type,name,pageNum,pageSize);
        return Response.ok(pageInfo);
    }
}
